<?php

namespace Admin\Pages;

/**
 * Description of Index
 *
 * @author nofear
 */
class Tags extends Page {

    private $objectEntity = 'Tag';

    public function __construct() {
        parent::__construct();
        $this->view = new \System\Template('@admin/tags.twig');
        $this->view->pageName = 'тагове';
    }

    public function index() {
        $QB = $this->EntityManager->createQueryBuilder();

        $objects = $QB->select('t')
                ->from($this->projectEntityNamespace . $this->objectEntity, 't')
                ->orderBy('t.id', 'DESC')
                ->getQuery()
                ->getResult();

        $Session = $this->getSession();
        if ($Session->have('message')) {
            $this->view->message = $Session->getMessage();
            $Session->removeMessage();
        }

        $this->view->objects = $objects;
    }

    public function delete($id) {
        $id = $this->sanitize($id);
        $Session = $this->getSession();

        $message['text'] = 'Възникна грешка!';
        $message['type'] = 'alert-danger';

        if ($id) {
            $Product = $this->EntityManager->find('Shared\Main\Tag', $id);

            if ($Product) {
                $this->EntityManager->remove($Product);
                $this->EntityManager->flush();

                $message['text'] = 'Тагът е изтрита успешно.';
                $message['type'] = 'alert-success';
            }
        }

        $Session->setMessage($message);
        $this->redirect(\Shared\URLManager::getInstance()->createPath('admin_tags'));
    }

}
